package org.voidness.oje2d;

/** A piece of text that disappears from screen after some time */
public class GLTimedText {
    /** The text */
    protected String text = null;

    /** The font to use */
    private GLFont font = null;

    /** Is the text dead? */
    protected boolean dead;

    /** The time the text will be alive */
    protected long timeToLive;

    /** The text's birth time */
    private long birthTime;

    /** The current time (for comparison) */
    private long currentTime;

    /**
     * Default constructor
     * 
     * @param mText The text to set
     * @param mFont The font to use
     * @param mTimeToLive The text's time to live
     */
    public GLTimedText(String mText, GLFont mFont, long mTimeToLive) {
        text = mText;
        font = mFont;
        timeToLive = mTimeToLive;

        currentTime = Utils.getTime();
        birthTime = currentTime;
        dead = false;
    }

    /**
     * (Re)defines the text
     * 
     * @param mText The text to set
     */
    public void setText(String mText) {
        text = mText;
    }

    /**
     * Returns the text
     * 
     * @return The text
     */
    public String getText() {
        return text;
    }

    /**
     * Returns whether the text is dead or not
     * 
     * @return True if it is, false otherwise
     */
    public boolean isDead() {
        return dead;
    }

    /**
     * Draws the piece of text on screen
     * 
     * @param mX The X component of the place where to draw
     * @param mY The Y component of the place where to draw
     * @param mZoom The zoom factor to use
     * @param mColor The color to use for the text
     */
    public void draw(int mX, int mY, float mZoom, GLColor mColor) {
        if (!dead) {
            if ((currentTime = Utils.getTime()) - birthTime > timeToLive)
                dead = true;

            font.print(text, mX, mY, mZoom, mColor);
        }
    }
}